package com.bjtu.restaurantreviews.handle;

import com.aliyuncs.exceptions.ClientException;
import com.bjtu.restaurantreviews.domain.Result;
import com.bjtu.restaurantreviews.exception.UserException;
import com.bjtu.restaurantreviews.util.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @Author: Chen
 * @Description: 统一异常处理类
 * @Date: Created in 22:15 2018/4/4
 */
@ControllerAdvice
public class ExceptionHandle {

    private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result handle(Exception e){
        if(e instanceof UserException){
            UserException userException = (UserException) e;
            return ResultUtil.error(userException.getMessage());
        }else {
            logger.error("[系统错误]",e);
            return ResultUtil.error("未知错误");
        }
    }
}
